package utilisateur;

import java.util.ArrayList;

import javax.persistence.*;
import javax.ejb.*;

/**
 * 
 * @author Geoffroy
 *
 */
@Stateless
@Local
public class RoleEJB implements RoleInterface {

	@PersistenceContext
	EntityManager em;
	
	
	public Role findById(int id) {
		return (Role)em.find(Role.class,id);
	}
	
	public Role findByName(String name) {
		Query q = em.createQuery("SELECT r FROM Role r WHERE r.nom=:r");
		q.setParameter("r", name);
		return (Role)q.getSingleResult();
	}
	
	public void save(Role r) {
		em.persist(r);
	}
	
	public void update(Role r) {
		em.merge(r);
	}
	
	public void delete(Role r) {
		em.remove(r);
	}

	public ArrayList<Role> getRoles() {
		return (ArrayList<Role>)em.createQuery("SELECT r FROM Role r").getResultList();
	}
	
}
